package com.secondshop.services;

import com.secondshop.mappers.GoodMapper;
import com.secondshop.models.Good;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodService {
	@Autowired
	private GoodMapper goodMapper;
	//分页查询查找所有商品
	@Transactional
	public List<Good> getAllGoods(int offset, int limit) {
		return goodMapper.getAllGoods(offset, limit);
	}
	//查找所有商品
	@Transactional
	public List<Good> getAllGoodList() {
		return goodMapper.getAllGoodList();
	}
	//查找推荐商品
	public List<Good> getGoodByStar(int star) {return  goodMapper.getGoodByStar( star); }
	//搜索查找
	@Transactional
	public List<Good> getGoodsBySearchAndType(String searchText,
//			Integer secondTypeId, int offset, int limit) 
	        Integer firstTypeId, int offset, int limit){
		if (!searchText.equals("")) {
			searchText = "%" + searchText + "%";
			return goodMapper.getGoodsBySearch(searchText, offset, limit);
		} else if (firstTypeId != null) {
			return goodMapper.getGoodsByType(firstTypeId, offset, limit);
		} else {
			return goodMapper.getAllGoods(offset, limit);
		}
		
//		if (!searchText.equals("")) {
//			searchText = "%" + searchText + "%";
//			return goodMapper.getGoodsBySearch(searchText, offset, limit);
//		} else if (secondTypeId != null) {
//			return goodMapper.getGoodsByType(secondTypeId, offset, limit);
//		} else {
//			return goodMapper.getAllGoods(offset, limit);
//		}
	}
	//统计
	@Transactional
	public int getGoodsBySearchAndTypeCount(String searchText,
			Integer secondTypeId) {
		if (!searchText.equals("")) {
			searchText = "%" + searchText + "%";
			return goodMapper.getGoodsBySearchCount(searchText);
		} else if (secondTypeId != null) {
			return goodMapper.getGoodsByTypeCount(secondTypeId);
		} else {
			return goodMapper.getAllGoodsCount();
		}
	}
	//根据goodId查找商品
	@Transactional
	public Good getGoodById(int goodId) {
		return goodMapper.getGoodById(goodId);
	}
	//根据goodId查找商品2
	@Transactional
	public Good getGoodById2(String goodId) {
		return goodMapper.getGoodById2(goodId);
	}
	//根据secondTypeId goodId查找商品
	@Transactional
	public List<Good> getRECGoods(int secondTypeId, int goodId) {
		return goodMapper.getRECGoods(secondTypeId, goodId);
	}
	//根据userid查找商品
	@Transactional
	public List<Good> getGoodByUserId(Integer userId) {
		return goodMapper.getGoodByUserId(userId);
	}
	//根据userid查找商品
	@Transactional
	public List<Good> getGoodStatusByUserId(Integer userId) {
		return goodMapper.getGoodStatusByUserId(userId);
	}
	//插入商品
	@Transactional
	public int insertGood(Good good) {
		return goodMapper.insterGood(good);
	}
	//插入商品推荐
	@Transactional
	public int insertStar(int goodId,int star) {
		
		return goodMapper.updateStar(goodId, star);
		
	}
	//更新图片的路径
	@Transactional
	public int updateGoodPhotoUrl(String photoUrl, Integer goodId) {
		return goodMapper.updateGoodPhotoUrl(photoUrl, goodId);
	}
	//更新商品状态
	@Transactional
	public int updateGoodStatusId(Integer statusId, Integer goodId) {
		return goodMapper.updateGoodStatusId(statusId, goodId);
	}
	//更新商品
	@Transactional
	public int updateGood(Good good) {
		return goodMapper.updateGood(good);
	}
	//获得商品图片
	@Transactional
	public String getPhotoUrlByGoodId(Integer goodId) {
		return goodMapper.getPhotoUrlByGoodId(goodId);
	}
	//删除商品
	@Transactional
	public int deleteGood(Integer goodId) {
		return goodMapper.deleteGood(goodId);
	}
	//根据分类查询商品
	@Transactional
	public List<Good> getGoodsAdminByType(Integer secondTypeId) {
		return goodMapper.getGoodsAdminByTypeId(secondTypeId);
	}
}
